package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.product.client.model.list.SearchParam;
import com.atguigu.gmall.product.client.model.list.SearchResponseVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
public class ListController {

    @Autowired
    private ListFeignClient listFeignClient;

    @GetMapping("/list")
    public String list( SearchParam searchParam, Model model){
        //回显页面  关键词
        model.addAttribute("searchParam",searchParam);
        //根据关键词 等相关条件查询的返回值对象
        SearchResponseVo responseVo = listFeignClient.search(searchParam);
        //品牌集合 trademarkList是前端需要传的数据
        model.addAttribute("trademarkList",responseVo.getTrademarkList());
        //URLParam   前端需要显示的动态超链接
        String urlParam=buildUrlParam(searchParam);
        model.addAttribute("urlParam",urlParam);
        //平台属性
        model.addAttribute("attrsList",responseVo.getAttrsList());
        //排序回显
        Map orderMap=buildOrderMap(searchParam);
        model.addAttribute("orderMap",orderMap);
        //结果集
        model.addAttribute("goodsList",responseVo.getGoodsList());
        //当前页
        model.addAttribute("pageNo",responseVo.getPageNo());
        //总页数
        model.addAttribute("totalPages",responseVo.getTotalPages());

        //选择品牌之后回显已选择结果
        String trademarkParam = buildTrademarkParam(searchParam);
        model.addAttribute("trademarkParam",trademarkParam);
        //选择平台属性之后回显已选择平台属性集合
        List<Map> propsParamList =buildPropsParamList(searchParam);
        model.addAttribute("propsParamList",propsParamList);

        return "list/index";
    }
    //选择平台属性之后回显已选择平台属性集合
    private List<Map> buildPropsParamList(SearchParam searchParam) {
        //props=23:4G:运行内存
        String[] props = searchParam.getProps();
        if (props != null && props.length > 0){
            return Arrays.stream(props).map(prop -> {
                String[] p = prop.split(":");
                Map map = new HashMap();
                map.put("attrId",p[0]);
                map.put("attrValue",p[1]);
                map.put("attrName",p[2]);
                return map;
            }).collect(Collectors.toList());
        }
        return null;
    }

    //选择品牌之后回显已选择结果
    private String buildTrademarkParam(SearchParam searchParam) {
        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)){

            String[] t = trademark.split(":");
            return "品牌:" +t[1];
        }
        return null;
    }

    //构建页面排序 红底样式回显  ASC DESC回显
    private Map buildOrderMap(SearchParam searchParam) {
        Map map = new HashMap<>();

        String order = searchParam.getOrder();
        if (!StringUtils.isEmpty(order)){
            String[] o = order.split(":");
            map.put("type",o[0]);
            map.put("sort",o[1]);
        }else{
            map.put("type",1);
            map.put("sort","asc");
        }
        return map;
    }


    //URLParam   前端需要显示的动态超链接
    private String buildUrlParam(SearchParam searchParam) {

        //StringBuilder  性能高  线程不安全  企业上班一般使用的是StringBuilder
        //因为此方法是在springIOC容器当中的  单例模式  当有线程调用此方法的时候 会复制一份到本地线程 让线程执行
        //并不会直接调用此方法 每次线程调用的都是此方法复制到本地线程的  所以使用的是stringBuilder
        //StringBuffer   性能滴  性能安全

        StringBuilder sb = new StringBuilder();
        //添加检索关键字
        String keyword = searchParam.getKeyword();
        if (!StringUtils.isEmpty(keyword)){
            sb.append("keyword=").append(searchParam.getKeyword());
        }
        //添加品牌
        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)){
            sb.append("&trademark=").append(searchParam.getTrademark());
        }
        //添加运行内存
        String[] props = searchParam.getProps();
        if (props != null && props.length>0){
            for (String prop : props) {
                sb.append("&props=").append(prop);
            }
        }
        //添加一级id
        Long category1Id = searchParam.getCategory1Id();
        if (category1Id !=null){
            sb.append("&category1Id=").append(searchParam.getCategory1Id());
        }
        //添加二级id
        Long category2Id = searchParam.getCategory2Id();
        if (category2Id !=null){
            sb.append("&category2Id=").append(searchParam.getCategory2Id());
        }
        //添加三级id
        Long category3Id = searchParam.getCategory3Id();
        if (category3Id !=null){
            sb.append("&category3Id=").append(searchParam.getCategory3Id());
        }


        return "/list?" + sb.toString();
    }

}
